# -*- coding: utf-8 -*-
"""
随机森林 和 extra-trees
Created on Sat Apr 28 16:34:48 2018

@author: Allen
"""

import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets

X, y = datasets.make_moons( n_samples = 500, noise = 0.3, random_state = 666 )

plt.scatter( X[y==0,0], X[y==0,1] )
plt.scatter( X[y==1,0], X[y==1,1] )
plt.show()

### 随机森林
# ensemble 集成学习
from sklearn.ensemble import RandomForestClassifier
rf_clf = RandomForestClassifier( n_estimators = 500, random_state = 666, oob_score = True )
rf_clf.fit( X, y )
print( rf_clf.oob_score_ ) # 0.892

### 使用 Extra-Tree
from sklearn.ensemble import ExtraTreesClassifier
et_clf = ExtraTreesClassifier( n_estimators = 500, bootstrap = True, oob_score = True )
et_clf.fit( X, y )
print( et_clf.oob_score_ ) # 0.896

### 集成学习解决回归问题
from sklearn.ensemble import BaggingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import ExtraTreesRegressor